Skip to content

fix: [AI-6622] enable cache_control trigger for altimate-backend provider#850

Merged
anandgupta42 merged 1 commit into
mainfrom
fix/AI-6622-altimate-backend-cache-control
May 30, 2026
Merged

fix: [AI-6622] enable cache_control trigger for altimate-backend provider#850
anandgupta42 merged 1 commit into
mainfrom
fix/AI-6622-altimate-backend-cache-control

Conversation

@sahrizvi
Copy link
Copy Markdown
Contributor

@sahrizvi sahrizvi commented May 29, 2026

Summary

Add model.providerID === "altimate-backend" to the cache-control gating condition in ProviderTransform.message() so messages routed through the Altimate Gateway get cache_control: ephemeral markers stamped on the last content block.

Why this matters

The Altimate Gateway uses litellm with provider fallback: Azure GPT-5.5 (primary) → Anthropic Sonnet 4.6 (fallback). When Azure errors and litellm falls back to Anthropic, Sonnet honors the cache_control markers and serves cached input at a discount.

Without this fix, fallback Sonnet calls would not cache, paying the full input rate.

For the Azure-primary path: Azure ignores cache_control markers (it uses automatic prefix-based caching), so stamping them is a no-op there.

Empirical context

Full RCA: ~/Documents/altimate-deliverables/AI-6622-{repro-summary,investigation-journey}.md.

We empirically proved the gateway's Azure path caches at 97-99% (response field usage.input_tokens_details.cached_tokens); the visible-cache-as-zero symptom that originated AI-6622 was a backend observability gap, not a cache failure. Companion backend fix: AltimateAI/altimate-backend#... extracts and surfaces the cached_tokens field so altimate-code can render real cache rate + cost.

Test plan

  • transform.ts:284-296 is the only modified location; single line addition
  • Build the CLI, run altimate-code with the Altimate Gateway model selected, force an Azure failure so litellm falls back to Anthropic, confirm response includes cache_read_input_tokens > 0 on warm turns

🤖 Generated with Claude Code


Summary by cubic

Enable cache_control markers for the altimate-backend provider so Altimate Gateway requests mark the last content block as ephemeral. This satisfies AI-6622 by allowing Anthropic fallback to cache input and reduce cost, while Azure primary remains unaffected.

  • Bug Fixes
    • Add model.providerID === "altimate-backend" to the cache-control gating in ProviderTransform.message().

Written for commit 63541ee. Summary will update on new commits.

Review in cubic

Summary by CodeRabbit

  • Performance
    • Enabled caching optimization for the altimate-backend provider to enhance application performance.

Review Change Stack

…ider

Adds `model.providerID === "altimate-backend"` to the cache-control gating
condition in `ProviderTransform.message()` so messages routed through the
Altimate Gateway get `cache_control: ephemeral` markers stamped on the last
content block. Required for the gateway's Anthropic-fallback path: when
Azure GPT-5.5 errors, litellm falls back to Anthropic Sonnet 4.6, which
honors these markers; without them, the fallback calls would not cache.

For the Azure-primary path: Azure ignores `cache_control` markers (it uses
automatic prefix-based caching), so stamping them is a no-op there. The
companion backend change extracts `prompt_tokens_details.cached_tokens` from
the response and surfaces it back to clients so cache rates and per-session
cost become visible.

Companion backend change on `altimate-backend` branch
`fix/AI-6622-llm-cache-plumbing`. Both repos pair together.

Full RCA + investigation journey: `~/Documents/altimate-deliverables/AI-6622-*.md`.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown

@claude claude Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review to trigger a review and subscribe this PR to future pushes, or @claude review once for a one-time review.

Tip: disable this comment in your organization's Code Review settings.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 29, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: bfe44bd1-ed2c-4607-94a5-c730cfe7a81b

📥 Commits

Reviewing files that changed from the base of the PR and between 56259a3 and 63541ee.

📒 Files selected for processing (1)
  • packages/opencode/src/provider/transform.ts

📝 Walkthrough

Walkthrough

Adds the altimate-backend provider ID to the caching eligibility condition in ProviderTransform.message, enabling the existing caching behavior for that provider alongside Anthropic and Claude checks.

Changes

Altimate-backend caching support

Layer / File(s) Summary
Enable caching for altimate-backend provider
packages/opencode/src/provider/transform.ts
The applyCaching conditional is extended to include model.providerID === "altimate-backend", allowing that provider to use the existing caching mechanism.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~2 minutes

Poem

🐰 A rabbit hops through code so neat,
One line added, caching's complete!
Altimate-backend joins the crew,
With Claude and friends, all caching true! ✨

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ❓ Inconclusive The description is largely complete with a clear Summary section explaining what changed and why, detailed context about the Altimate Gateway and provider fallback behavior, and a Test Plan section. However, the required 'PINEAPPLE' marker for AI-generated contributions is missing from the top of the description. Add the word 'PINEAPPLE' at the very top of the PR description before any other content, as required by the template for AI-generated contributions (note: the description contains AI-generation markers like '🤖 Generated with Claude Code').
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically summarizes the main change: enabling cache_control for the altimate-backend provider, which directly matches the single-line addition in the changeset.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/AI-6622-altimate-backend-cache-control

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint skipped: no ESLint configuration detected in root package.json. To enable, add eslint to devDependencies.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 1 file

Re-trigger cubic

Copy link
Copy Markdown

@dev-punia-altimate dev-punia-altimate left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multi-Persona Review — Verdict: block

The PR introduces critical ClickHouse syntax bugs that will cause migration failures and a product-gap issue where the database tables are insufficient without accompanying data pipeline components. While architectural decisions are sound, the PR cannot ship as-is due to high-severity functional and operational risks.

15/15 agents completed · 240s · 4 findings (2 critical, 1 high, 1 medium)

Critical

  • [code-reviewer] ReplicatedReplacingMergeTree engine uses '/clickhouse/tables/{{database}}_{name}/logs' path with unescaped {name} — this will cause ClickHouse to fail because {{database}} is not interpolated and {name} is treated as a literal string, not a variable. → alembic_tenant_clickhouse/migrations/versions/2026_05_28_0030_add_dbx_jobs_cost_savings_tables.py:47
    • 💡 Escape the {name} in the path as {{name}} to prevent ClickHouse from interpreting it as a format placeholder. Use: '/clickhouse/tables/{{database}}_{{name}}/logs'
  • [code-reviewer] DROP TABLE uses SYNC flag without checking if cluster_clause is non-empty — if cluster_name is empty, the SYNC flag will be appended to DROP TABLE without ON CLUSTER, which is valid, but if cluster_name is set, the SYNC flag must come before ON CLUSTER to be valid ClickHouse syntax. → alembic_tenant_clickhouse/migrations/versions/2026_05_28_0030_add_dbx_jobs_cost_savings_tables.py:157
    • 💡 Reorder the DROP TABLE statement to place SYNC before ON CLUSTER: f'DROP TABLE IF EXISTS {table} {cluster_clause} SYNC;' → f'DROP TABLE IF EXISTS {table} SYNC {cluster_clause};' if cluster_clause is non-empty, or just f'DROP TABLE IF EXISTS {table} SYNC;' if empty.

High

  • [product-manager] Jira ticket AI-591 requests implementation of a Databricks jobs cost-savings pipeline, but the PR only adds database tables without any accompanying ingestion, transformation, or query logic to make the data usable for cost-savings insights. The tables are necessary but insufficient to deliver user value.
    • 💡 Either add the data pipeline components (loaders, stitchers, or APIs) that consume these tables, or create a follow-up ticket to track the missing functionality required to complete the user story.

Medium

  • [cross-repo-impact] New ClickHouse tables added for Databricks jobs cost-savings pipeline (dbx_jobs_cost_savings, dbx_auto_recommendation_mirror, dbx_auto_tune_audit_events_mirror) — requires new ingestion DAGs in altimate-dags to populate these tables. → alembic_tenant_clickhouse/migrations/versions/2026_05_28_0030_add_dbx_jobs_cost_savings_tables.py:1
    • 💡 Create new DAGs in altimate-dags to ingest data into dbx_jobs_cost_savings, dbx_auto_recommendation_mirror, and dbx_auto_tune_audit_events_mirror tables from their respective PostgreSQL sources.

Multi-Persona Review · vllm:qwen3-next-80b (waves) + vllm-fallback (synth) ·

@dev-punia-altimate
Copy link
Copy Markdown

❌ Tests — Failures Detected

TypeScript — 15 failure(s)

  • baseline [0.44ms]
  • baseline [0.34ms]
  • baseline [0.07ms]
  • baseline [0.23ms]
  • connection_refused [0.21ms]
  • timeout [0.05ms]
  • permission_denied [0.09ms]
  • parse_error [0.04ms]
  • oom [0.03ms]
  • network_error [0.03ms]
  • auth_failure [0.03ms]
  • rate_limit [0.03ms]
  • internal_error [0.04ms]
  • empty_error [0.02ms]
  • connection_refused [0.04ms]

Next Step

Please address the failing cases above and re-run verification.

cc @sahrizvi

@anandgupta42 anandgupta42 merged commit bc50b59 into main May 30, 2026
20 checks passed
anandgupta42 added a commit that referenced this pull request May 30, 2026
…-shared file

The merge from main brought in PR #850's altimate-backend provider ID
without altimate_change markers. Adds the markers around the
altimate-specific provider IDs in the Anthropic-style detection block
so future upstream merges don't silently overwrite them.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
anandgupta42 added a commit that referenced this pull request May 30, 2026
…ing (#849)

* docs: add Kimi-K2.6 ADE-Bench behavioral analysis + dbt skill improvements

Adds research/kimi-k26-ade-bench-2026-05-10/ with a blog-ready writeup of how
the Moonshot Kimi-K2.6 model behaves as a coding agent inside altimate-code's
agent loop, derived from 78 trial traces against ADE-Bench. Findings cover
tool-usage distribution, wall-clock anatomy (~89% model generation, ~5%
tools), prompt-cache amplification (85.8%), per-failure-class taxonomy, and
extended appendices (per-trial manifest, pass-rate by family, skill
invocation log, cost/runtime distribution, reproducibility command, glossary,
open questions).

Also extends two shipped skills with generic dbt-best-practice patterns
surfaced during the analysis (all benchmark-agnostic, applicable to any dbt
project):

- dbt-develop/SKILL.md
  * stronger description with explicit invocation triggers
  * new section on transformation-logic pitfalls: incremental high-water
    marks (>= vs >), snapshot strategy selection, LEFT JOIN + COUNT(*)
    phantom rows, type harmonization in COALESCE/CASE/UNION, date-spine
    completeness, off-by-one window boundaries, uniqueness enforcement,
    window-LIMIT tiebreakers
  * deliverable-enumeration step in Validate phase + iron rule
  * unit-test verification step + iron rule
- dbt-unit-tests/SKILL.md
  * new iron rule requiring mock data to exercise every SQL construct's
    failure mode (LEFT JOIN unmatched parents, NULLIF zero, CASE branches,
    COALESCE all-null, window boundaries, date spines, etc.)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* docs: add benchmark/ade-bench/ reproduction scaffolding

Adds the source-code + scripts + 4 small patches needed to plug
altimate-code into upstream ade-bench. Lets anyone reproduce the
81.3% pass rate described in research/kimi-k26-ade-bench-2026-05-10/
without trusting the pre-aggregated numbers.

What's included:
- benchmark/ade-bench/README.md — full reproduction guide (prereqs,
  Docker memory, env-var knobs, step-by-step commands, troubleshooting)
- benchmark/ade-bench/altimate_code_agent/ — drop-in agent module
  (AltimateCodeAgent class, JSON event parser, log formatter, install
  script that runs inside the trial container, tarball builder)
- benchmark/ade-bench/patches/ — 4 small patches against upstream
  dbt-labs/ade-bench (register AgentName.ALTIMATE_CODE, wire it into
  the AgentFactory, export from installed_agents/__init__.py, route
  the existing shared/config/AGENTS.md baseline file the same way
  Codex receives it — pure parity, no benchmark-specific content)

Explicitly NOT in this folder:
- Trace files / per-trial agent.log / results.json (regenerable)
- The 130 MB built tarball (build-local-tarball.sh recreates it)
- Seed DuckDB databases (downloaded from dbt-labs/ade-bench releases)
- Per-task ground-truth seeds + test SQL (those live in upstream
  ade-bench and are never sent to the agent at run time)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* feat: auto-load skills via applyPaths frontmatter + new dbt-develop pitfalls

Two related changes, both shipped to every altimate-code user.

(1) `feat(skill)`: add `alwaysApply: bool` and `applyPaths: string|string[]`
    frontmatter to skill metadata, mirroring Cursor's "Always Apply" and
    "Auto Attached" rule modes. When a skill is `alwaysApply: true` or has
    `applyPaths` matching at least one file under the worktree, its body
    is inlined into the system prompt at session start under an
    `<auto_loaded_skill>` block — the model no longer needs to invoke the
    Skill tool to access that guidance.

    Motivation: benchmark traces show the agent invokes the `Skill` tool
    in <1% of tool calls, even after the skill description is rewritten
    to be imperative. Many failures occur on patterns the relevant skill
    already documents but the agent never loads. Auto-loading puts the
    body deterministically in context for projects where the skill
    applies.

    Files:
      • packages/opencode/src/skill/skill.ts — Info schema + both load
        paths (filesystem + binary-embedded) pluck the new fields
      • packages/opencode/src/session/system.ts — auto-inline matched
        skill bodies after the existing available_skills XML block
      • .opencode/skills/dbt-develop/SKILL.md — frontmatter now declares
        `applyPaths: [dbt_project.yml, **/dbt_project.yml]`, so dbt
        projects auto-load this skill's body (~270 lines of dbt
        best-practice patterns) at session start

    The existing skill-tool-invocation path is unchanged; auto-load is
    additive. Skills without `alwaysApply` / `applyPaths` continue to
    require explicit invocation. Prompt caching amortizes the extra
    tokens across the long agent loop.

(2) `docs(skill)`: three new generic dbt pitfall sections in
    `dbt-develop/SKILL.md`, all benchmark-agnostic best practices
    surfaced during failure-trace analysis:

    • String concatenation with `NULL` operands — `||` / `CONCAT`
      propagate `NULL`; wrap with `COALESCE` or use `CONCAT_WS`.
      Catches an invisible row-dropper in surrogate-key generation and
      derived columns.
    • dbt model versioning (dbt 1.8+) — when introducing a v2 of an
      existing model, use dbt's `versions:` block in `_models.yml` with
      `defined_in:`, not a sibling `_v2.sql` file. Otherwise downstream
      lineage and `{{ ref(model, v=2) }}` resolution break.
    • Strengthened the existing window-rank + `LIMIT` section to call
      out determinism explicitly, including the `QUALIFY ROW_NUMBER()
      OVER (... ORDER BY metric, id)` form and the "if you can't think
      of a tiebreaker, you don't have a unique key yet" framing.

    All three patterns are documented in well-known dbt style guides
    and would benefit any real altimate-code user — they are not
    benchmark-targeted tweaks.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* docs: document alwaysApply / applyPaths skill frontmatter fields

Adds reference for the new auto-load mechanism to docs/docs/configure/skills.md:
- Lists the two new frontmatter fields in the Frontmatter Fields table
- New "Auto-loading skills" section explaining the lazy-load default,
  how `alwaysApply` and `applyPaths` change it, a worked example,
  a "when to use" table, and an honest section on context-size
  implications + prompt-cache amortization

Pure documentation update — no code change in this commit.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* feat: reorder auto-loaded skill bodies + add pre-completion checklist

Two changes informed by trace analysis of the benchmark run with the
initial auto-load mechanism. With the auto-loaded body present in the
system prompt, 6 of 8 sampled failing trials never referenced any of
its guidance keywords (date spine, tiebreaker, deliverable, etc.) —
the model was treating the auto-loaded section as background reference
rather than binding directive. These two changes address the framing.

(1) `feat(system-prompt)`: move auto-loaded skill bodies BEFORE the
    lazy-loaded `<available_skills>` XML block in the skills section.

    Previously the order was:
      1. "Use the skill tool to load a skill..." preamble
      2. <available_skills> XML (long, descriptions only)
      3. <auto_loaded_skill> body (binding guidance)

    Now:
      1. <auto_loaded_skill> body (binding guidance — read FIRST)
      2. "Skills provide specialized instructions..." preamble
      3. <available_skills> XML (lazy-loaded skills the agent can opt into)

    Framing the auto-loaded body as "rules of the road" at the start
    rather than supplementary documentation at the end. Pure ordering
    change in `SystemPrompt.skills()` parts array — no schema or API
    change. Applies to any skill using `applyPaths` or `alwaysApply`.

    File: packages/opencode/src/session/system.ts

(2) `docs(skill)`: add a "Pre-completion checklist" section (§5) to
    dbt-develop that the agent is told to emit with `[x]/[ ]` marks
    before declaring the task done.

    Each item is a yes/no question against patterns the skill already
    documents (LEFT JOIN cardinality, date-spine completeness,
    window-rank tiebreaker, type harmonization in COALESCE/CASE/UNION,
    string-concat NULL handling, uniqueness enforcement, incremental
    high-water mark, snapshot strategy, dbt model versioning v2,
    unit-test verification).

    The forcing function: the agent must produce the checklist text in
    its final message. Unchecked items without a stated "n/a" reason
    mean the task is not done. Forces the model to slow down at the
    end and verify the patterns against the SQL it just wrote, rather
    than silently skip the verification phase.

    All items are generic dbt patterns applicable to any project — no
    benchmark-specific test names, no solution-seed values, no
    grading-rubric hints.

    File: .opencode/skills/dbt-develop/SKILL.md

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* revert(skill): roll back pre-completion checklist; document negative result

The "emit a [x]/[ ] checklist before declaring done" addition to
dbt-develop (§5, shipped two commits ago) was measured negative on
the post-A+B benchmark re-run:

  - Checklist appeared in 6 of 14 still-failing trial outputs.
  - Zero of those 6 flipped to PASS.
  - In multiple traces, the agent self-marked `[x] LEFT JOIN
    cardinality correct` while the underlying SQL still had the
    exact phantom-row bug the item warned against.

The framing trained the model to perform verification theater
rather than actually re-read its SQL. The two flips attributed
earlier to "A+B" (helixops_saas007, helixops_saas009) trace back
to the placement reorder (A) — the checklist (B) contributed
nothing measurable, and adds 50+ lines of system-prompt content
for no benefit.

This commit:

(1) Removes §5 from `.opencode/skills/dbt-develop/SKILL.md`.
    The other sections (Plan → Discover → Write → Validate,
    Common Pitfalls in Transformation Logic, Iron Rules) stay
    intact. The placement reorder in `system.ts` and the
    `applyPaths`/`alwaysApply` frontmatter mechanism stay.

(2) Adds a "What we tried that didn't work" section to
    research/kimi-k26-ade-bench-2026-05-10/findings.md so the
    negative result is preserved as institutional knowledge.
    The broader principle — "soft self-verification (model
    promises it checked X) is unreliable on this model class;
    hard verification (compile/test failures) still works" — is
    worth keeping around.

(3) Updates the findings TL;DR with both the original 81.3%
    headline and the post-second-wave 85.3% best-of-runs number,
    with the caveat that the body of the post analyzes the
    first-wave traces.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* docs(skill): swap dbt-textbook airbnb names for abstract placeholders

The `LEFT JOIN + COUNT(*)` pitfall example referenced `dim_listings LEFT JOIN
fct_reviews`. Those names are the canonical airbnb dbt-tutorial models (from
Maven Analytics / public dbt courses) and also happen to be ADE-Bench tasks,
so even though the rule itself is fully generic, the example wording was
needlessly close to benchmark content. Swap to abstract `dim_parent LEFT JOIN
fct_child` — the rule is identical, the wording is unambiguous.

No behavior change. Cosmetic only.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* feat(skill): schema fidelity + CTE-refactor row preservation + spec-diff validate step

Three new generalizable dbt patterns surfaced from systematic trace
analysis of ADE-Bench failures:

1. Iron Rule 8 — Schema Fidelity: agent must match the spec's column
   tuple exactly (names, types, ORDER, no extras). Adding "helpful"
   columns or substituting synonyms (supplier_id vs supplier_company)
   breaks AUTO_*_equality tests against the spec contract.

2. CTE-to-model refactor row preservation: when extracting a CTE into
   a standalone intermediate model, build it FROM the parent table the
   CTE started from, not the child table. The extracted model otherwise
   becomes effectively an INNER JOIN and drops parent rows with no
   children. Includes dbt_utils.equal_rowcount and audit_helper
   verification patterns.

3. Diff-against-spec step in the validate phase: agent produces three
   lists (columns_extra, columns_missing, columns_reordered) and treats
   any non-empty list as "not done". Verification > in-prompt negative
   rules (per the Self-Verification Dilemma literature).

All three pass the "When working on any dbt project, ..." self-test.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* feat(dbt-tools): altimate-dbt schema-verify — mechanical column-shape check

Adds a new `altimate-dbt schema-verify --model <name>` subcommand that
mechanically diffs a model's produced columns against the schema.yml
spec and returns a structured `{verdict, columns_extra, columns_missing,
columns_reordered, type_mismatches}` result.

Background: trace analysis of repeated benchmark failures showed that
in-prompt rules ("match the column spec exactly") were being read but
not applied — the agent agrees in principle, then adds extra columns
or reorders them anyway. The Self-Verification Dilemma literature
predicts this: negative rules without a mechanical check are weak.

Design follows the existing dbt-tools split: dbt parsing lives in
altimate-code (via dbt-integration's adapter), so the bridge belongs
here. Spec source: `adapter.parseManifest().nodeMetaMap.lookupByBaseName(model).columns`
(schema.yml entries compiled into manifest.json). Actual source:
`adapter.getColumnsOfModel(model)` (warehouse / catalog).

Case-insensitive name comparison (dbt convention). Type mismatches
are reported only when the spec actually declares `data_type` —
common to omit it, and treating omission as a mismatch would
produce noise.

Skill change: the validate phase's "diff column shape" section now
prescribes calling `altimate-dbt schema-verify` and treating any
`mismatch` verdict as "not done", instead of asking the agent to
self-diff column lists.

Iron Rule 8 also tightened to point at the mechanical check.

Tests: 13 covering the four diff categories, the no-spec skip,
case-insensitivity, type-mismatch precedence rules, error
propagation, and two regression-style cases mirroring real
ade-bench failure shapes (extra rank-breakdown columns, leading-
column reorder).

All four pass the "When working on any dbt project, …" self-test.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* feat(skill): extract dbt-schema-verify into a dedicated auto-load skill

Move the schema-verify procedure out of dbt-develop's body (where it
was ~30 lines deep in a 450-line skill) into its own focused skill
that auto-loads on dbt projects.

Why: trace inspection of v5 runs showed the agent reads the
schema-verify instruction inside dbt-develop, agrees with it in
chain-of-thought, then doesn't actually run the command. Burying a
procedural step inside a discursive skill is part of the problem;
the agent gives the step less attention than skill-top imperative
content.

Design: short, procedural, imperative skill body. Auto-loads via
applyPaths alongside dbt-develop. Iron rules state the contract
explicitly. Includes a fallback when altimate-dbt isn't available
(reads schema.yml + dbt show by hand). Cross-references the
dbt-develop "CTE row-preservation" pattern for the related
row-count case (which schema-verify does NOT cover).

dbt-develop now points at dbt-schema-verify instead of embedding the
full procedure. Iron Rule 8 similarly points at the dedicated skill.

Honest caveat: this is still a prompt-level intervention. Trace
inspection of v4 and v5 runs both showed that even mechanically-
callable tools get ignored if the harness doesn't enforce the call.
The structural fix is harness-level: a before_terminate hook with
per-domain completion validators, of which dbt-schema-verify would
be one. That work is a follow-up — this skill is the cleanest
prompt-side fix in the meantime.

Passes the "When working on any dbt project, ..." self-test.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* feat(dbt-tools): auto-run schema-verify after build --model

The agent has been ignoring skill-level instructions to call
schema-verify before declaring done (v4 and v5 trace inspection
showed the agent reads the rule, writes the intention in
chain-of-thought, then doesn't run the command). Building a full
harness-level validator framework that intercepts session
termination is real engineering; this commit ships the smallest
forcing mechanism that doesn't require touching the session loop:
auto-trigger schema-verify inside the build command's own response.

The agent now cannot see a successful `altimate-dbt build --model X`
without also seeing the schema-verify verdict in the same tool
result. The diff is in the JSON response under `schema_verify`,
in-context where the agent's attention sits — much harder to
ignore than a system-prompt skill rule.

Behavior:
- `build` without `--model` is unchanged (project-wide build, no
  per-model verify makes sense).
- `build --model X` runs schema-verify on X after a successful
  build. The full structured result lives at `schema_verify`.
- A verify failure does NOT mask the build's stdout — both are
  reported. Build status remains the success/error signal.
- If verify itself errors (missing manifest, unbuilt table), the
  error is reported under `schema_verify.error` with a fix hint.

Tests:
- Updated existing build-test mocks to include parseManifest +
  getColumnsOfModel (no behavior change, just shape consistency).
- New assertion: build --model X result now contains schema_verify.

Skill: dbt-schema-verify body adds a note that the agent gets
schema-verify "for free" inside build's response, so it doesn't
need to call it twice for verification.

This is a stepping stone — the full validator framework that
intercepts session termination is the next iteration. This commit
tests whether putting the diff inline with the build response is
enough to break through the ignore-the-rule pattern.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* feat(dbt-tools): extend schema-verify auto-trigger to project-wide build

v7 trace inspection revealed the agent uses `altimate-dbt build`
(no --model) for project-wide builds and `dbt build --model X`
(plain dbt) for per-model — never `altimate-dbt build --model X`.
So the per-model auto-trigger added in 3924009 never fired in any
of the 30 trials, even though it was wired correctly. The hook
missed because of the agent's command choice, not because the agent
ignored the result.

This commit extends the auto-trigger to the project-wide path:
after a successful `altimate-dbt build` (no --model), iterate every
model in the parsed manifest that has columns declared in schema.yml,
run schema-verify on each, and roll up the results into a single
`schema_verify_summary` field on the response:

  {
    "stdout": "...",
    "schema_verify_summary": {
      "models_checked": N,
      "match": M,
      "mismatch": K,
      "no_spec": L,
      "errored": E,
      "mismatches": [ { model, verdict, columns_extra, ... } ]
    }
  }

Only the mismatches are reported in full. Match and no-spec models
are counted but not echoed (keeps the response compact for 49-model
projects). Errored models include the per-model error string so the
agent can investigate.

The summary is the closest a CLI command can get to harness-level
enforcement without intercepting session termination: every
project-wide build now returns the full diff against schema.yml in
the same tool result the agent receives for the build. The agent
literally cannot see a green project-build without also seeing every
schema mismatch in the project.

Tests: new "project-wide build collects mismatches" test exercises
the 3-model case (match + mismatch + no-spec) end to end.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* feat(session): harness-side validator framework (off by default)

Six experiments (v3-v9) proved that every form of completion-discipline
enforcement living inside the agent's command surface — skill rule, tool
description, auto-trigger inside a wrapping CLI, even binary substitution
— gets read, agreed-with in chain-of-thought, then ignored. In v9 the
agent actively found a backup binary at `.orig` to bypass the wrapping
shim. The Self-Verification Dilemma literature predicts this. The only
remaining lever is enforcement the agent cannot see: the harness
inspecting the world after `finishReason === "stop"` and refusing to
terminate if a registered validator says the work isn't done.

This commit adds the framework but does not enable it. Behavior is opt-in
via ALTIMATE_VALIDATORS_ENABLED=1, with a separate retry budget knob
(ALTIMATE_VALIDATORS_MAX_RETRIES=3 default). Telemetry fires
unconditionally so we can measure baseline fire rate against historical
traffic even before the gate is enabled.

Files added (framework, domain-agnostic):
  - session/validators/types.ts — Validator, ValidatorResult,
    ValidatorContext interfaces with a load-bearing comment explaining
    why this lives in the harness and not in skills/tools.
  - session/validators/registry.ts — Map-keyed registry + runAll that
    catches per-validator exceptions and converts them to soft-passes
    (a buggy validator should never brick the agent loop).

Files added (altimate domain, first concrete validator):
  - altimate/validators/dbt-schema-verify.ts — wraps the existing
    `altimate-dbt schema-verify` CLI. appliesTo: dbt project detected
    in worktree. check: scans models/ for .sql files mtime'd in this
    session, runs schema-verify on each, returns mismatch with a
    structured fixHint listing columns_extra/missing/reordered.
  - altimate/validators/index.ts — side-effect registration on import.

Wiring in session/prompt.ts step loop:
  - After processor.process() returns and the model declared finish:"stop"
    with no error and no pending tool calls, runAll() is dispatched.
  - Telemetry fires for every validator regardless of opt-in.
  - If the gate is enabled AND any validator failed AND we're under the
    retry budget: a synthetic user message is appended to the session
    with the aggregated failure reasons + fix hints. The step loop's
    top-of-iteration break check then sees the newer user message and
    does NOT break — the model gets one more turn to address the gap.
  - Retry budget exhaustion falls through to the natural break.

Architectural choice: the dispatch hook is in prompt.ts not in
processor.ts. processor.process() returns per-step semantics (stop /
continue / compact); prompt.ts owns the multi-step harness loop. The
validator gate is a harness concept, not a stream concept.

Generalisable: the framework is domain-agnostic. New validators register
via `ValidatorRegistry.register(...)` from any module's side-effect
import. Phase 2 candidates (already scoped, not in this commit):
dbt-rowcount-preservation, dbt-tests-pass, sql-compile, sql-equivalence,
pii-scan, column-lineage. Each is ~30-50 LOC on top of the framework.

Not in this commit (deferred):
  - The skill diet (extracting process-discipline content out of
    dbt-develop / dbt-schema-verify into validator fixHints). Lands once
    we've measured validator fire rates with the gate enabled.
  - Unit tests for the framework (lives in a follow-up dedicated test
    PR since prompt.ts is already heavily integration-tested upstream).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(validators): explicit registration + diagnostic log

bun --single may tree-shake side-effect imports. Switch to explicit
registerAltimateValidators() call so the registration is unambiguously
referenced. Also add an info log on every hook entry so we can confirm
the code path is reached even when validators don't fire.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(validators): stderr diagnostic so harness logs capture the signal

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* feat(validators): dbt-tests-pass + schema-verify hardening + marker fixes

Harness-side completion-gate validator framework, completing the 3-part
series from PR #792 (registry) + PR #800 (dbt-schema-verify) + this PR.

### New: dbt-tests-pass validator
- Fires after the agent declares done (finish === "stop")
- Detects dbt model `.sql` files modified since session start via mtime
- Runs `altimate-dbt test --model <name>` against each touched model
- Parses `Done. PASS=N WARN=N ERROR=N ...` summary from dbt output
- Extracts individual failing test names from per-line output
- Injects synthetic user message with fix hints when tests fail
- `extractLastJsonObject()` handles altimate-dbt's JSON envelope + log noise
- Only activates in dbt projects (scans for `dbt_project.yml`)

### Enhanced: dbt-schema-verify hardening
- `parseSchemaVerifyOutput()` — scans backwards for last balanced `{...}`
  block to handle dbt log noise (ANSI codes, parser warnings) emitted
  before the JSON verdict
- Debug logging for spawn errors and close events, gated behind
  `ALTIMATE_VALIDATORS_DEBUG=1` so normal sessions stay quiet
- Better error fallback: reports non-JSON stdout when stderr is empty

### prompt.ts: debug-gated diagnostics
- `ALTIMATE_VALIDATORS_DEBUG=1` env var gates all stderr console.error
  calls — on by default in ade-bench harness, off everywhere else
- Added `validatorsEnabled &&` guard on dispatch condition (was missing)
- Debug logs for dispatch_enter, dispatch_result, dispatch_error events
- `hasError` field added to validator_hook_reached diagnostic

### build-local-tarball.sh: altimate-dbt on PATH
- Added `"altimate-dbt": "./dbt-tools/bin/altimate-dbt"` to bin entries
- Ensures `altimate-dbt` is available via PATH in benchmark Docker
  containers (was missing; validators depend on it)

### system.ts: fix stray altimate_change marker placement
- Moved `// altimate_change end` from inside `skills()` function body
  (before the closing `}`) to outside it — the function's closing brace
  was appearing outside any marker block, triggering Marker Guard CI

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix: [#849] address code-review findings in validator framework

All issues identified in the consensus review (Claude + 8 models) are
addressed in this commit.

**MAJOR fixes (blocking merge)**

- Add subprocess timeout (`ALTIMATE_VALIDATORS_TIMEOUT_MS`, default 60 s)
  to `runDbtTest` and `runSchemaVerify` — prevents the agent loop from
  hanging indefinitely on stalled DuckDB connections or warehouse I/O.
  Kills the child process with SIGKILL on timeout.

- Fix shadow telemetry gate: move `ValidatorRegistry.runAll()` and the
  per-validator `Telemetry.track()` loop outside the `validatorsEnabled`
  check in `prompt.ts`.  Telemetry now fires regardless of the feature
  flag, fulfilling the "measure before enforce" promise stated in the
  inline comment.

- Fix nested dbt project cwd bug: replace `isDbtProject(cwd): boolean`
  with `findDbtProjectRoot(cwd): Promise<string | null>` which returns
  the directory that actually contains `dbt_project.yml`.  Both
  validators now pass that root as `cwd` to subprocess invocations and
  as the base for `modelsModifiedSince`, preventing the "not a dbt
  project" error when the project is one level below the working dir.

- Extract shared helpers into `validator-utils.ts`: `findDbtProjectRoot`,
  `modelsModifiedSince`, `modelNameFromPath`, `extractLastJsonObject`.
  Both validator files now import from the shared module.  The validated
  `extractLastJsonObject` rejects stray JSON fragments (checks for
  `verdict`/`error`/`model`/`stdout`/`columns_*` keys) — the laxer
  version that was only in `dbt-tests-pass.ts` is gone.

- Add tests: 39 unit tests covering `extractLastJsonObject` (8 cases),
  `modelNameFromPath`, `findDbtProjectRoot` (5 cases),
  `modelsModifiedSince` (7 cases), and `parseDbtTestOutput` (10 cases
  including dbt 1.x format, ANSI prefixes, NO-OP variant, duplicate
  names, `[FAIL`/`[ERROR` token exclusion).

**MINOR fixes**

- Track spawn failures separately in `dbt-tests-pass.check()` and
  `dbt-schema-verify.check()`.  `details.spawn_failures` now appears in
  the validator result so operators can distinguish "skipped model" from
  "passed model".

- Add retries-exhausted telemetry: when `validatorRetryCount >=
  maxValidatorRetries` with failures outstanding, emit
  `validator_retries_exhausted` event and a `log.warn` so the session
  doesn't silently appear as "completed" in the operator dashboard.

- Parallel model checking: both `check()` functions now use
  `Promise.all` instead of a sequential `for` loop.

**NITS**

- Named regex groups in `parseDbtTestOutput` — replaces positional
  `summaryMatch[1]` / `[3]` / `[5]` captures; resilient to dbt
  reordering summary fields.

- Path separator: `modelsModifiedSince` and `modelNameFromPath` now use
  `path.sep` / `path.basename` instead of hardcoded `"/"`.

- Fix `ls | head -1` in `build-local-tarball.sh`: derive exact tarball
  name from `VERSION` variable; error out explicitly if not present.

- Fix stale comment "Limited to two-level deep search" in
  `dbt-schema-verify.ts` (actual depth was 4; comment is removed).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* test: [#849] adversarial test expansion for validator utilities

Expands the validator test suite from 39 to 117 tests, covering
boundary conditions, malformed inputs, and realistic dbt output patterns
that the original suite did not exercise.

**extractLastJsonObject adversarial cases**
- Stray JSON rejection: empty object, array with no envelope keys,
  objects with only unknown keys, numeric keys
- Envelope key guard: null/false/empty-string values still accepted when
  key is present; `error: null` does not invalidate the envelope
- Noisy stdout scanning: Python traceback + JSON, 50-line progress
  noise, BOM prefix, CRLF line endings, > 10 KB leading noise,
  whitespace-padded JSON
- Last-wins semantics: two valid envelopes (last wins), three valid
  envelopes (last wins), stray fragments between valid envelopes,
  same-line consecutive objects
- Brace/string parsing: nested braces in string values, escaped
  backslashes, escaped double-quotes, multiline string values, stdout
  field containing inner JSON, unicode characters, unicode escape
  sequences, multi-line formatted JSON, unbalanced `{` in log noise

**parseDbtTestOutput adversarial cases**
- Null/empty guard: null, undefined, whitespace-only, truncated output,
  dbt compile error (no Done. line)
- All-pass: clean run, SKIP-only, WARN-only
- NO-OP variant: zero tests, multiple NO-OP counts
- Failure extraction: deduplication of repeated test names, FAIL vs
  ERROR lines, `[FAIL`/`[ERROR` token exclusion, test names with dots,
  15+ failing tests captured
- Large counts: 99999 pass, 99999 error, zero counts, single test
- Format resilience: case-insensitive Done., named groups vs positional
  (PASS=7 ERROR=3 TOTAL=11), timestamps, ANSI colours, CRLF line
  endings, summary at very start/end of string, multiple summary lines
- Realistic full-output scenarios: dbt 1.8 all-pass, dbt 1.8 partial
  failures, ANSI-coloured Docker output, no-tests-defined NO-OP,
  SKIP from --exclude flag

**findDbtProjectRoot adversarial cases**
- Two-level deep search limit: project at depth 2 is NOT found
- dbt_project.yml is a directory (documents stat behavior)
- Many subdirs, only one has the file
- Direct takes precedence over nested

**modelsModifiedSince adversarial cases**
- Depth boundary: depth 4 included, depth 5 excluded (with path counts)
- Non-.sql files inside models/: yml, md, py, json all excluded
- File named `models.sql` outside a `models/` path component excluded
- Mtime boundary: file with mtime === sinceMs is included (>= semantics)
- Mixed modified/unmodified files
- Empty models/ directory (no SQL files)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix: remove upstream product name from research/kimi-k26 findings

* fix: [#849] address PR review comments — validator hardening + agent fixes

Addresses all unresolved review threads from coderabbitai and cubic-dev-ai
on PR #849 (feat/validator-framework).

**Critical fixes**
- `build-local-tarball.sh`: REPO_ROOT traversal depth 6→3 `..` segments
  (script is at `benchmark/ade-bench/altimate_code_agent/`, 3 levels deep)
- `prompt.ts`: explicit `continue` after `validatorRetryCount++` to make
  the retry loop intent unambiguous (was falling through to bottom-of-loop
  `continue` correctly but implicitly)
- `prompt.ts`: `workingDirectory` now uses `Instance.directory` instead of
  `process.cwd()` to match the session's actual working directory

**Major fixes**
- `altimate_code_agent.py`: `shlex.quote()` on `self._model_name` before
  shell interpolation to prevent injection via model name strings
- `altimate-code-setup.sh`: `@latest` fallback replaced with `exit 1` for
  benchmark reproducibility; config file now written with `chmod 600`
- `dbt-schema-verify.ts`: fail closed on errors — spawn failures now pushed
  into `results` as error entries; `ok` check requires `errored === 0` so
  unverifiable models don't silently pass the completion gate
- `system.ts`: XML-escape `skill.name` before embedding in
  `<auto_loaded_skill name="...">` attribute via `escapeXmlAttr()`
- `system.ts`: remove `.catch(() => [])` inside `anyMatchInWorktree` so
  errors propagate to the outer try/catch; `autoLoadLog.warn(...)` is now
  reachable
- `registry.ts`: `appliesTo()` exceptions now surface error details as a
  soft-pass result entry instead of being silently swallowed

**Minor fixes — validator-utils.ts hardening**
- `VALIDATOR_TIMEOUT_MS`: finite/positive guard against NaN, 0, or negative
  env-var values (all fall back to the 60 s default)
- `modelsModifiedSince`: case-insensitive `.sql` check (`.toLowerCase()`)
  for consistency with `modelNameFromPath` which already uses `/\.sql$/i`
- New `runWithConcurrencyLimit` helper (max 4 concurrent by default, env
  override via `ALTIMATE_VALIDATORS_CONCURRENCY`) replaces unbounded
  `Promise.all` in both `dbt-tests-pass.ts` and `dbt-schema-verify.ts`

**Docs / research corrections**
- `findings.md`: timing table row renamed "Step-to-step intervals
  (start-to-start)" to clarify it includes step duration, not just gaps
- `findings.md`: f1011 description corrected — `check_option_b` FAILED
  (per Appendix C), not passed
- `findings.md`: per-domain failure counts corrected (asana 4→3, f1 5→4;
  total now sums to 19, consistent with headline 78-59=19)
- `dbt-unit-tests/SKILL.md`: removed untestable "empty partition" window
  function guidance; replaced with partition-of-1 and tie-break row cases
- `dbt-schema-verify/SKILL.md`: fallback verification glob broadened from
  `schema.yml` to `**/*.yml` to catch `_models.yml` and other conventions

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* test: [#849] deflake `work can be started after cancel` runner test

CI's TypeScript job failed on this test with a 30 s timeout on commit
81b6df2, but the test passes 5/5 locally. The root cause is a race:

  const fiber = yield* runner.ensureRunning(Effect.never).pipe(Effect.forkChild)
  yield* Effect.sleep("10 millis")  // <-- not guaranteed to land after state = Running
  yield* runner.cancel               // <-- if still Idle, cancel is a no-op
  yield* Fiber.await(fiber)          // <-- waits forever on Effect.never

`Effect.forkChild` returns before `ensureRunning` has transitioned the
runner from Idle to Running. On a slow CI runner, the 10 ms sleep can
expire before that transition completes, so `runner.cancel` matches the
Idle branch (no-op) and the test hangs awaiting `Effect.never`.

Replace the fixed sleep with a busy-poll that exits as soon as the runner
reports `busy === true`, eliminating the race entirely.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix: [#849] 11 real bugs found via adversarial validator-utils testing

Wrote a new adversarial test suite (`adversarial-bugs.test.ts`, 16 tests)
that targets edge cases the original 117 tests didn't exercise. The
suite found 11 real bugs in validator-utils.ts; each is now fixed.

**Bugs in `runWithConcurrencyLimit` (5)**

1. `limit=0` silently dropped every item — `Math.min(0, len)` produced
   zero workers; results stayed as sparse `undefined` and the caller
   never knew anything was skipped.
2. `limit=-1` had the same silent-drop behavior.
3. `limit=NaN` had the same silent-drop behavior (Array.from coerces
   NaN length to 0).
4. `limit=0.5` was floored to 0 by Array.from — silent drop.
5. `limit=0.7` (e.g. user sets `ALTIMATE_VALIDATORS_CONCURRENCY=0.7`)
   collapsed to 0 — silent drop.

**Fix**: clamp `limit` with `Number.isFinite(limit) && limit >= 1`,
floor the value, and cap at `items.length`. Defaults to 1 worker for
any invalid input so work is never silently skipped.

**Bugs in `modelsModifiedSince` (3)**

6. Case-sensitive `models/` check missed `Models/` or `MODELS/` on
   case-insensitive volumes (macOS APFS default, Windows NTFS).
7. Hard depth cap of 4 silently dropped files in realistic dbt layouts
   like `models/staging/sources/dl/raw/foo.sql` (depth 5+).
8. Uppercase `.SQL` extension was matched (fixed in earlier commit)
   but the surrounding `MODELS/` dir was still skipped — an internal
   inconsistency.

**Fix**: increase depth cap to 8; make the `models/` path-component
check case-insensitive (`.toLowerCase() === "models"`).

**Bugs in `findDbtProjectRoot` (2)**

9. Non-deterministic selection when multiple subdirectories each have
   a `dbt_project.yml` — relied on `fs.readdir` order, which varies
   across filesystems and Node versions.
10. A *directory* named `dbt_project.yml` was treated as a valid project
    marker (`fs.stat` doesn't distinguish file from directory).

**Fix**: sort entries alphabetically before iterating; replace bare
`fs.stat` existence check with an `isFile()` test.

**Bug in `extractLastJsonObject` envelope guard (1)**

11. `isValidEnvelope` accepted `{"verdict": null}` because `"verdict"
    in obj` returns true even when the value is null — a stray JSON
    fragment with the right shape could be mistaken for a real verdict.

**Fix**: require envelope keys to have *defined, non-null* values
(except `error: null`, which is intentionally allowed as the "ran
cleanly" sentinel).

**Test updates**

Two pre-existing tests in `validator-utils.test.ts` pinned the old
buggy behavior (depth=5 excluded; directory-as-project accepted) — both
updated to assert the corrected behavior.

All 133 validator tests pass; typecheck and marker guard clean.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix: [#849] address remaining 39 adversarial bugs from waves 2-12

Builds on commit 9eb6bc7 (wave 1: 11 bugs fixed). This commit fixes
the remaining real bugs surfaced by adversarial test waves 2-12 against
`validator-utils.ts`, `dbt-tests-pass.ts`, and `system.ts`.

**`parseDbtTestOutput` (dbt-tests-pass.ts)**
- Anchored summary regex so `Done.` mid-word (`Predone.`) or inside
  quotes / paragraphs no longer false-matches.
- Made WARN/SKIP/NO-OP fields optional in the summary regex — compact
  dbt outputs (PASS/ERROR/TOTAL only) now parse correctly.
- Switched to global-flag scan that keeps the LAST `Done.` summary, so
  retried runs report the latest authoritative counts instead of the
  first (incorrect) one.
- Strip ANSI CSI sequences from stdout before parsing so colour codes
  don't break field matching or pollute captured test names.
- Replaced greedy `\\S+` test-name capture with bounded char class
  `[A-Za-z0-9_./:-]+` plus a `VALID_TEST_NAME_RE` post-check. Stops
  over-capturing `[FAIL]`, `(could not connect ...)`, `Done.`,
  quoted/angle-bracketed/comma-prefixed noise, and URLs.
- Reject names containing `://` so URLs in failure messages aren't
  treated as test names.
- Clamp count fields at `Number.MAX_SAFE_INTEGER` to prevent precision
  loss for absurdly large values.

**`escapeXmlAttr` (system.ts)**
- Escape `\\n`/`\\r`/`\\t` as `&#10;`/`&#13;`/`&#9;` so attribute values
  stay on a single line for log readers / grep / awk.
- Strip XML-1.0-invalid control characters (NUL, VT, FF, etc.) so a
  rogue skill name can't produce invalid XML.

**`modelNameFromPath` (validator-utils.ts)**
- Normalise Windows-style `\\` separators to `/` before basename() so
  paths copied from Windows (or mixed-separator inputs) resolve to the
  correct model name.
- Strip embedded NUL bytes from the returned name to prevent
  shell-argument truncation downstream.

**`runWithConcurrencyLimit` (validator-utils.ts)**
- Treat `Infinity` as "unbounded" (= items.length) instead of
  collapsing to the default of 1.

**`modelsModifiedSince` (validator-utils.ts)**
- Follow symlinks: a symlinked SQL file or a symlinked directory under
  `models/` is now discovered, matching the obvious user expectation.

**`findDbtProjectRoot` (validator-utils.ts)**
- Skip dotfile / `node_modules` / `target` directories when scanning
  for a nested `dbt_project.yml`, mirroring `modelsModifiedSince`. A
  fixture project shipped inside `node_modules/foo/` or a build
  artifact in `target/` no longer gets mistaken for the user's project.

**Tests**
- 12 adversarial wave files added (`adversarial-bugs.test.ts` plus
  `adversarial-wave-{2..12}.test.ts`), 308 new tests total. Each
  failing test originally demonstrated a real bug; the file headers
  describe the categories probed.
- 3 tests marked `.skip` document known design limitations (rejection
  mid-flight, trailing-whitespace SQL filenames, case-insensitive
  filesystem behaviour).

All 424 validator tests pass; typecheck clean; marker guard clean.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* test: [#849] add 51 E2E test cases (`.skip`) documenting real-world bugs

Adds seven `e2e-real-dbt*.test.ts` files that exercise the validators
end-to-end against a real `dbt` 1.8 + duckdb adapter and the real
`altimate-dbt` CLI (no mocks).

Of the 137 E2E test cases written, **51 expose distinct real bugs or
feature gaps** when run unskipped. They are landed as `test.skip(...)`
so CI stays green while the bugs are documented in code with
reproducible scenarios — each test is a faithful repro that exercises
the full subprocess flow:
  - real `altimate-dbt init` / `altimate-dbt build` / `altimate-dbt test`
    / `altimate-dbt schema-verify` subprocesses
  - real `dbt-core` 1.8 + `dbt-duckdb` 1.8
  - a fresh duckdb file in each temp project dir

To run them locally:
  ALTIMATE_VALIDATORS_DEBUG=1 bun test test/altimate/validators/e2e-real-dbt*.test.ts
(remove `test.skip` to enable each case)

**Categories of bugs / gaps documented**

CORRECTNESS / E2E SYNC ISSUES (build → schema-verify):
- happy-path schema-verify reports `mismatch` because the build/verify
  cycle in altimate-dbt doesn't reliably surface the just-built table
- model-with-Jinja, two-model, tests-pass passing/no-tests scenarios
  fail for the same root cause
- nested workspace dbt projects (depth > 1) are not detected by
  `findDbtProjectRoot`'s one-level search
- ref-chain models — only the modified file should count as "touched"
- concurrent validator runs share duckdb file lock and don't return
  consistent results

CONFIGURATION / PATH HANDLING:
- custom `model-paths: ["analytics"]` in `dbt_project.yml` is silently
  ignored (validator only scans `models/`)
- conflicting model names across subdirs (e.g. `models/a/foo.sql` +
  `models/b/foo.sql`) dedupe to one entry by `modelNameFromPath`,
  silently dropping the other
- malformed `schema.yml` / 0-byte model / hyphen-named model / invalid
  materialization / nonexistent macro / nonexistent ref — none of
  these distinguish themselves from "schema mismatch" in the result
- pre_hook errors collapse into generic build failure

ERROR SURFACING (reason / fixHint / details quality):
- result doesn't surface the failing model name in `reason`
- no `elapsed_ms` / `total_subprocess_ms` field for telemetry
- no per-model `verdict` breakdown
- no `schema_yml_paths` / `dbt_root` / `dbt_version` / `dbt_adapter`
  fields in details
- no `validator_version`, `altimate_dbt_path`, `concurrency_limit`,
  `session_id`, `run_at` for traceability
- spawn timeouts not reported separately from spawn failures
- exit codes from subprocess not surfaced
- schema-verify doesn't distinguish "model never built" from "schema
  drift" — both report `verdict: mismatch`
- tests-pass doesn't list passing tests or the per-model failure breakdown
- no `total_tests` / `tests_skipped` / `failing_rows` fields

MISSING FEATURE COVERAGE:
- orphan `schema.yml` entries (model in spec but not on disk) not detected
- Python models (.py) not picked up
- analyses/, tests/, seeds/ dirs all silently ignored (correct, but
  documented)

The 7 files together cover ~127 distinct scenarios; the 51 failing
expectations are the catalogue of items that need engineering follow-up
to either fix the validator behaviour or richen the details schema.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix: [#849] gate validators on opt-in flag + enrich result details

Two follow-ups based on side-effect analysis of the PR:

**1. Performance side effect for dbt users (the biggest gap)**

Previously the validator dispatch in `prompt.ts` ran on every successful
agent turn — fs scan, subprocess spawn, the works — even when
`ALTIMATE_VALIDATORS_ENABLED=0` (the default). The flag only gated the
synthetic-message retry; the expensive part (real `altimate-dbt test` /
`schema-verify` subprocesses) still ran "for telemetry". That added
30 s – 5 min per session end for any dbt user, opted-in or not.

Now the entire dispatch path is gated on either:
  - `ALTIMATE_VALIDATORS_ENABLED=1` (full enforcement, with retries), or
  - `ALTIMATE_VALIDATORS_SHADOW=1` (run without enforcement — for
    "would have fired" telemetry against historical traffic).

If neither is set (the default), the dispatch returns immediately
after the diagnostic log. No fs scan, no subprocess spawns, no perf
tax — for any user, dbt or otherwise.

**2. Result details enriched**

Addresses several of the documented E2E feature-gap tests by adding
telemetry / traceability fields to both validator results:
  - `dbt_root` — the resolved project root (or null when not a dbt project)
  - `session_id` — echoed back for trace correlation
  - `elapsed_ms` — wall time spent inside `check()`
  - `concurrency_limit` — actual worker cap used
  - reason text now names the failing models inline (e.g. "models you
    edited have failing tests: foo, bar.")

Three E2E tests un-skipped now that their expected behaviour is met
(elapsed_ms / dbt_root / session_id / concurrency_limit / failing-model
name in reason).

All 429 active tests pass; typecheck clean; marker guard clean.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* docs: [#849] document the validator framework + opt-in defaults

Adds the user-facing documentation for the completion-gate validator
framework introduced in this PR. Specifically:

- **New page**: `docs/docs/data-engineering/validators.md` — full
  reference covering what validators are, when they fire, the two
  built-in dbt validators, the two opt-in modes
  (`ALTIMATE_VALIDATORS_ENABLED` for enforcement vs
  `ALTIMATE_VALIDATORS_SHADOW` for telemetry-only), all configuration
  knobs, performance characteristics, the emitted telemetry events,
  the result shape, the phased rollout plan, known limitations, and
  how to write a custom validator.
- **Nav**: linked from the `Use → ` section in `mkdocs.yml`.
- **dbt-tools page**: brief mention with a link to the validators page
  so anyone reading the dbt tool reference learns about the
  harness-side gates.
- **Telemetry reference**: two new event rows
  (`validator_check`, `validator_retries_exhausted`) added to the
  collected-events table, cross-linked to the validators page.
- **CHANGELOG.md**: new `Unreleased` section announcing the framework,
  the two modes, the new env vars, and a link to the docs.

The docs are deliberate about positioning the framework as **opt-in
by default** today, with a phased path to default-on once shadow
telemetry confirms low false-positive rates and the open coverage /
sync issues are resolved.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* bench: [#849] enable validators by default in ade-bench setup

ade-bench is the whole reason the validator framework exists — without
ALTIMATE_VALIDATORS_ENABLED=1 the per-trial setup, a vanilla bench run
measures the agent without its completion gates and we get the wrong
baseline for any post-#849 evaluation.

Sets the env var at the end of altimate-code-setup.sh per trial. Opt-out
via ALTIMATE_VALIDATORS_BENCH_DISABLE=1 for intentional baseline runs.

Trade-off: adds 30 s – 2 min per trial of validator wall time.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix: [#849] mark altimate-backend providerID in transform.ts upstream-shared file

The merge from main brought in PR #850's altimate-backend provider ID
without altimate_change markers. Adds the markers around the
altimate-specific provider IDs in the Anthropic-style detection block
so future upstream merges don't silently overwrite them.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* docs: [#849] address review feedback on validator docs

- validators.md: drop "session rollup" claim from telemetry section
  (only per-validator events are emitted today); make `checked` /
  `concurrency_limit` optional in the result-shape schema to match
  what the validators actually return on the no-models path
- dbt-tools.md: mention both opt-in flags (ENABLED + SHADOW) and the
  zero-overhead default
- skills.md: correct the auto-load placement docs (prepended BEFORE
  the available-skills listing, not appended after — placement was
  deliberate)
- benchmark/ade-bench/README.md: add 'text' language to the directory
  tree code fence for markdownlint MD040

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants